// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package
{
    import starling.display.Image;
    import starling.display.QuadBatch;
    import starling.textures.Texture;
    import starling.textures.TextureSmoothing;

    public class Scale9QuadBatch extends QuadBatch
    {
        private var mImageWidth:int;
        private var mImageHeight:int;

        private var mMinImageWidth:int;
        private var mMinImageHeight:int;

        private var mTopLeftImgTexture:Texture;
        private var mTopRightImgTexture:Texture;
        private var mBottomLeftImgTexture:Texture;
        private var mBottomRightImgTexture:Texture;
        private var mMiddleImgTexture:Texture;
        private var mTopMiddleImgTexture:Texture;
        private var mLeftMiddleImgTexture:Texture;
        private var mRightMiddleImgTexture:Texture;
        private var mBottomMiddleImgTexture:Texture;

        public function Scale9QuadBatch(_width:int,
                                        _height:int,
                                        _topLeft:Texture,
                                        _topRight:Texture,
                                        _bottomLeft:Texture,
                                        _bottomRight:Texture,
                                        _middle:Texture,
                                        _topMiddle:Texture,
                                        _leftMiddle:Texture,
                                        _rightMiddle:Texture,
                                        _bottomMiddle:Texture)
        {

            mImageWidth = _width;
            mImageHeight = _height;
            mTopLeftImgTexture = _topLeft;
            mTopRightImgTexture = _topRight;
            mBottomLeftImgTexture = _bottomLeft;
            mBottomRightImgTexture = _bottomRight;
            mMiddleImgTexture = _middle;
            mTopMiddleImgTexture = _topMiddle;
            mLeftMiddleImgTexture = _leftMiddle;
            mRightMiddleImgTexture = _rightMiddle;
            mBottomMiddleImgTexture = _bottomMiddle;

            mMinImageWidth = mTopLeftImgTexture.width + mTopRightImgTexture.width;
            mMinImageHeight = mTopLeftImgTexture.height + mBottomLeftImgTexture.height;

            super();

            refreshDisplay();
        }

        //==================================
        //== Public Function
        //==================================

        public function changeSize(_width:int, _height:int):void
        {
            _width = Math.max(_width, mMinImageWidth);
            _height = Math.max(_height, mMinImageHeight);
            if (mImageWidth != _width || mImageHeight != _height)
            {
                mImageHeight = _height;
                mImageWidth = _width;
                refreshDisplay();
            }
        }


        //==================================
        //== Override Function
        //==================================

        //================
        //== Height
        //================
        override public function get height():Number {return mImageHeight;}

        override public function set height(value:Number):void
        {
            value = Math.max(value, mMinImageHeight);
            if (mImageHeight != value)
            {
                mImageHeight = value;
                refreshDisplay();
            }
        }

        //================
        //== Width
        //================
        override public function get width():Number {return mImageWidth;}

        override public function set width(value:Number):void
        {
            value = Math.max(value, mMinImageWidth);
            if (mImageWidth != value)
            {
                mImageWidth = value;
                refreshDisplay();
            }
        }

        //==================================
        //== Private Function
        //==================================

        private function refreshDisplay():void
        {
            reset();
            
            var middleHorizontalSize:int = Math.max(0, mImageWidth - mMinImageWidth);
            var middleVerticalSize:int = Math.max(0, mImageHeight - mMinImageHeight);


            var topLeftImg:Image = new Image(mTopLeftImgTexture);
            topLeftImg.smoothing = TextureSmoothing.NONE;

            var topRightImg:Image = new Image(mTopRightImgTexture);
            topRightImg.smoothing = TextureSmoothing.NONE;

            var bottomLeftImg:Image = new Image(mBottomLeftImgTexture);
            bottomLeftImg.smoothing = TextureSmoothing.NONE;

            var bottomRightImg:Image = new Image(mBottomRightImgTexture);
            bottomRightImg.smoothing = TextureSmoothing.NONE;

            var middleImg:Image = new Image(mMiddleImgTexture);
            middleImg.smoothing = TextureSmoothing.NONE;

            var topMiddleImg:Image = new Image(mTopMiddleImgTexture);
            topMiddleImg.smoothing = TextureSmoothing.NONE;

            var leftMiddleImg:Image = new Image(mLeftMiddleImgTexture);
            leftMiddleImg.smoothing = TextureSmoothing.NONE;

            var rightMiddleImg:Image = new Image(mRightMiddleImgTexture);
            rightMiddleImg.smoothing = TextureSmoothing.NONE;

            var bottomMiddleImg:Image = new Image(mBottomMiddleImgTexture);
            bottomMiddleImg.smoothing = TextureSmoothing.NONE;


            //==================================
            //==Middle 比理像素+2,并偏移(-1,-1)
            //==================================
            middleImg.x = mTopLeftImgTexture.width - 1;
            middleImg.y = mTopLeftImgTexture.height - 1;
            middleImg.width = middleHorizontalSize + 2;
            middleImg.height = middleVerticalSize + 2;
            addImage(middleImg);


            //==============
            //==LeftMiddle
            //==============
            leftMiddleImg.x = 0;
            leftMiddleImg.y = mTopLeftImgTexture.height;
            leftMiddleImg.height = middleVerticalSize;
            addImage(leftMiddleImg);

            //================
            //==RightMiddle
            //================
            rightMiddleImg.x = mImageWidth - mTopRightImgTexture.width;
            rightMiddleImg.y = leftMiddleImg.y;
            rightMiddleImg.height = middleVerticalSize;
            addImage(rightMiddleImg);

            //=============
            //==TopMiddle
            //=============
            topMiddleImg.x = mTopLeftImgTexture.width;
            topMiddleImg.y = 0;
            topMiddleImg.width = middleHorizontalSize;
            addImage(topMiddleImg);

            //================
            //==BottomMiddle
            //================
            bottomMiddleImg.x = topMiddleImg.x;
            bottomMiddleImg.y = mImageHeight - mBottomLeftImgTexture.height;
            bottomMiddleImg.width = middleHorizontalSize;
            addImage(bottomMiddleImg);

            //============
            //==TopLeft
            //============
            topLeftImg.x = topLeftImg.y = 0;
            addImage(topLeftImg);


            //==============
            //==TopRight
            //==============
            topRightImg.x = mImageWidth - topRightImg.width;
            topRightImg.y = 0;
            addImage(topRightImg);


            //================
            //==BottomLeft
            //================
            bottomLeftImg.x = 0;
            bottomLeftImg.y = height - bottomLeftImg.height;
            addImage(bottomLeftImg);

            //===============
            //==BottomRight
            //===============
            bottomRightImg.x = topRightImg.x;
            bottomRightImg.y = bottomLeftImg.y;
            addImage(bottomRightImg);

            topLeftImg.dispose();
            topRightImg.dispose();
            bottomLeftImg.dispose();
            bottomRightImg.dispose();
            middleImg.dispose();
            leftMiddleImg.dispose();
            rightMiddleImg.dispose();
            topMiddleImg.dispose();
            bottomMiddleImg.dispose();

        }


    }
}
